<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ obj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="obj.aut_name" :label="fn1(obj.pubdate)">
        <template #icon>
          <img :src="obj.aut_photo" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              v-if="obj.is_followed"
              type="info"
              size="mini"
              @click="fn(true)"
              >已关注</van-button
            >
            <van-button
              v-else
              icon="plus"
              type="info"
              size="mini"
              plain
              @click="fn(false)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="obj.content"></div>

      <!-- 分割线 -->
      <div id="add">
        <van-divider>End</van-divider>
      </div>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          @click="fn2(-1)"
          v-if="obj.attitude === 1"
          icon="good-job"
          type="danger"
          size="small"
          >已点赞</van-button
        >
        <van-button
          @click="fn2(1)"
          v-else
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          >点赞</van-button
        >
      </div>
    </div>
    <CommentList />
  </div>
</template>

<script>
import {
  articleDetailAPI,
  followedUserAPI,
  unFollowedUserAPI,
  LikingsAPI,
  NolikingsAPI
} from '@/api/home.js'
import { Toast } from 'vant'
import moment from 'moment'
import CommentList from './CommentList'
export default {
  components: {
    CommentList
  },
  data () {
    return {
      obj: {}
    }
  },
  async created () {
    const res = await articleDetailAPI(this.$route.params.kws)
    this.obj = res.data
  },
  methods: {
    async fn (add) {
      try {
        add
          ? await unFollowedUserAPI(this.obj.aut_id)
          : await followedUserAPI(this.obj.aut_id)
        this.obj.is_followed = !add
        Toast.success(add ? '取消关注' : '关注成功')
      } catch (error) {
        Toast.fail('关注失败')
      }
    },
    fn1 (date) {
      // moment.locale('zh-cn')默认是英文,修改中文
      moment.locale('zh-cn')
      // 传入以前时间,调用当前时间获取需要的多长时间前
      return moment(date).from(new Date())
    },
    async fn2 (sum) {
      try {
        this.obj.attitude === 1
          ? await NolikingsAPI(this.$route.params.kws)
          : await LikingsAPI(this.$route.params.kws)
        this.obj.attitude = sum
        Toast.success(this.obj.attitude === 1 ? '点赞成功' : '取消点赞')
      } catch (error) {
        Toast.fail('点赞失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
